﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using MAT.Model;


namespace MAT.Controllers
{
    class ctrFindAsset
    {
        private frmFindAsset _frmFindAsset = null;

        public frmFindAsset FrmFindAsset
        {
            get { return _frmFindAsset; }
            set { _frmFindAsset = value; }
        }
        MAT_Context context = MATContext.getContext();
        public void btnFindAsset_Click()
        {
            _frmFindAsset.lvFindAsset.Items.Clear();
            string A = _frmFindAsset.cmbCategoryName.SelectedValue.ToString().Trim();
            List<CATEGORY> category = context.CATEGORies.Where(a => a.CATEGORY_NAME == A).ToList();
            foreach (CATEGORY cat in category)
            {
                List<ASSET> assets = context.ASSETs.Where(a => a.CATEGORY_ID == cat.CATEGORY_ID).ToList();
                foreach (ASSET asset in assets)
                {

                    ListViewItem list = new ListViewItem(asset.ASSET_ID);
                    ListViewItem.ListViewSubItem sub1 = new ListViewItem.ListViewSubItem(list, asset.ASSET_NAME);
                    list.SubItems.Add(sub1);
                    ListViewItem.ListViewSubItem sub2 = new ListViewItem.ListViewSubItem(list, asset.MODEL_NUM);
                    list.SubItems.Add(sub2);
                    ListViewItem.ListViewSubItem sub3 = new ListViewItem.ListViewSubItem(list, asset.BRAND);
                    list.SubItems.Add(sub3);
                    List<OWNER> owners = context.OWNERS.Where(o => o.EMPLOYEE_ID == asset.EMPLOYEE_ID).ToList();
                    foreach (OWNER owner in owners)
                    {
                        ListViewItem.ListViewSubItem sub4 = new ListViewItem.ListViewSubItem(list, owner.EMPLOYEE_NAME);
                        list.SubItems.Add(sub4);
                        List<ROOM> rooms = context.ROOMs.Where(r => r.ROOM_ID == owner.ROOM_ID).ToList();
                        foreach (ROOM room in rooms)
                        {
                            ListViewItem.ListViewSubItem sub5 = new ListViewItem.ListViewSubItem(list, room.ROOM_NAME);
                            list.SubItems.Add(sub5);
                        }

                        List<SUPPLIER> suppliers = context.SUPPLIERs.Where(s => s.SUPPLIER_ID == asset.SUPPLIER_ID).ToList();
                        foreach (SUPPLIER sup in suppliers)
                        {
                            ListViewItem.ListViewSubItem sub6 = new ListViewItem.ListViewSubItem(list, sup.SUPPLIER_NAME);
                            list.SubItems.Add(sub6);
                        }

                    }


                    _frmFindAsset.lvFindAsset.Items.Add(list);
                   
                }

            }
          }
            
        public void frmFind_Load()
        {
            _frmFindAsset.cmbCategoryName.DataSource = context.CATEGORies.ToList();
            _frmFindAsset.cmbCategoryName.DisplayMember = "CATEGORY_NAME";
            _frmFindAsset.cmbCategoryName.ValueMember = "CATEGORY_NAME";
            _frmFindAsset.lvFindAsset.Columns.Add("Asset ID", 100, HorizontalAlignment.Left);
            _frmFindAsset.lvFindAsset.Columns.Add("Asset Name", 100, HorizontalAlignment.Left);
            _frmFindAsset.lvFindAsset.Columns.Add("Model Number", 100, HorizontalAlignment.Left);
            _frmFindAsset.lvFindAsset.Columns.Add("Brand", 100, HorizontalAlignment.Left);
            _frmFindAsset.lvFindAsset.Columns.Add("Owner", 100, HorizontalAlignment.Left);
            _frmFindAsset.lvFindAsset.Columns.Add("Room", 100, HorizontalAlignment.Left);
            _frmFindAsset.lvFindAsset.Columns.Add("Supplier", 100, HorizontalAlignment.Left);
            _frmFindAsset.lvFindAsset.GridLines = true;
            _frmFindAsset.lvFindAsset.FullRowSelect = true;
           
        }

        public void btnClose_click()
        {
            _frmFindAsset.Close();
            MAT.Controllers.ctrMain._frmMain.pnlMain.Visible = false;
        }
    }

}


